package com.woniu.service.impl;

import com.woniu.model.EUser;
import com.woniu.dao.EUserMapper;
import com.woniu.model.Page;
import com.woniu.service.IEUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Kevin Kwok
 * @since 2021-04-13
 */
@Service
@Transactional
public class EUserServiceImpl extends ServiceImpl<EUserMapper, EUser> implements IEUserService {
    @Resource
    private EUserMapper eUserMapper;

    //查询当前页的用户信息
    @Override
    public List<EUser> getCurrentPageUser(int pageIndex, int pagesize) {
        //当前页的查询起始位置
        int start = (pageIndex - 1) * pagesize;
        List<EUser> eUsers = eUserMapper.queryCurrentPageUsers(start, pagesize);
        return eUsers;
    }

    //封装page对象，用来给分页做准备
    @Override
    public Page getPageInfo(int pageIndex, int pageSize) {
        Page page = new Page();
        page.setPageIndex(pageIndex);
        int i = eUserMapper.countAllRecords();
        System.out.println("总条数："+i);
        page.setDataCount(i);
        page.setPageSize(pageSize);
        page.setPageCount(i % pageSize == 0 ? i / pageSize : i / pageSize + 1);
        return page;
    }

    @Override
    public int frozenAccount(Integer id) {
        int i = eUserMapper.frozenAccount(id);
        return i;
    }

    @Override
    public int editAccount(Integer id, String idNumber, Integer status) {
        System.out.println("id:::"+idNumber);
        //参数均不为空
        if(idNumber!=null && status!=null){
                int i = eUserMapper.editIdNumber(id, idNumber);
                int j = eUserMapper.editStatus(id, status);
                return i+j;
        }
        //只修改账号状态
        if(idNumber=="" && status!=null){
                int i = eUserMapper.editStatus(id, status);
                return i;
        }
        //只修改身份证
        if(idNumber!=null && status==null){
                int i = eUserMapper.editIdNumber(id, idNumber);
                return i;
        }
        //参数均为空
        if(idNumber=="" && status==null){
                return 0;
        }
        return 0;
    }
}
